package com.fin.quant.web.controller;

import com.fin.quant.app.user.LoginService;
import com.fin.quant.app.user.vo.LoginRequest;
import com.fin.quant.app.user.vo.LoginResult;
import com.fin.quant.rpc.common.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * 登录控制器
 */
@RestController
@Slf4j
public class LoginController {

    @Autowired
    private LoginService loginService;

    /**
     * 用户登录
     */
    @PostMapping(value = "/user/login", consumes = "application/x-www-form-urlencoded")
    public Result<LoginResult> login(@ModelAttribute LoginRequest request) {
        log.info("收到登录请求: {}", request);
        return loginService.login(request);
    }

    /**
     * 发送短信验证码
     */
    @PostMapping("/user/send_verify_code")
    public Result<LoginResult> sendVerifyCode(@RequestParam String phone,
                                              @RequestParam(defaultValue = "+86") String countryCode) {
        log.info("收到发送验证码请求: phone={}, countryCode={}", phone, countryCode);
        return loginService.sendVerifyCode(phone, countryCode);
    }

    /**
     * 健康检查
     */
    @GetMapping("/health")
    public Result<String> health() {
        return Result.createSuccessResult("登录服务正常");
    }
}
